package com.songyuquan.webcs.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songyuquan.webcs.mapper.OrderMapper;
import com.songyuquan.webcs.pojo.Orders;
import com.songyuquan.webcs.service.OrdersService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrdersService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void daochu(HttpServletResponse response) {
        String excelName = "订单管理表";
        List<Orders> orders = orderMapper.selectList(null);
        ExcelWriter writer = ExcelUtil.getBigWriter();
        StyleSet style = writer.getStyleSet();
        CellStyle headCellStyle = style.getHeadCellStyle();
        //水平居中
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //设置背景填充色
        headCellStyle.setFillForegroundColor(IndexedColors.WHITE.index);
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headCellStyle.setFillBackgroundColor(IndexedColors.WHITE.index);
        headCellStyle.setWrapText(true);
        //设置内容字体
        Font font = writer.createFont();
        //字体颜色
        font.setColor(IndexedColors.BLACK.getIndex());
        //字体大小
        font.setFontHeightInPoints((short) 12);
        //加粗
        font.setBold(true);
        //
        font.setFontName("仿宋_GB2312");
        //设置标题字体大小
        headCellStyle.setFont(font);

        style.setBackgroundColor(IndexedColors.WHITE, true);
        style.setFont(IndexedColors.BLACK.getIndex(), (short) 10, "宋体", true);
        style.setWrapText();


        CellStyle cellStyle = writer.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillBackgroundColor(IndexedColors.WHITE.index);
        cellStyle.setWrapText(true);
        Font font0 = writer.createFont();
        font0.setColor(IndexedColors.BLACK.getIndex());
        font0.setFontHeightInPoints((short) 24);
        font0.setBold(true);
        font0.setFontName("方正小标宋简体");
        cellStyle.setFont(font0);

        AtomicInteger i = new AtomicInteger(1);
        List<Map<String, String>> collect = orders.stream().map(mm -> {
//            StringBuilder jczt = new StringBuilder();
//            Integer tmmadTestStatus = mm.getTmmadTestStatus();
//            if (tmmadTestStatus == 1) {
//                jczt.append("阴性");
//            } else if (tmmadTestStatus == 2) {
//                jczt.append("阳性");
//            } else if (tmmadTestStatus == 3) {
//                jczt.append("未检测");
//            } else {
//                jczt.append("未知");
//            }
//            StringBuilder dbzt = new StringBuilder();
//            String tmmadType = mm.getTmmadType();
//            if (tmmadType.equals("0")) {
//                dbzt.append("自办");
//            } else if (tmmadType.equals("1")) {
//                dbzt.append("代办");
//            } else {
//                dbzt.append("未知");
//            }
            Map<String, String> li = new HashMap<>();
            li.put("序号", i.getAndIncrement() + "");
            li.put("用户编号", String.valueOf(mm.getUserId()));
            li.put("用户名", mm.getLoginName());
            li.put("用户地址", mm.getUserAddress());
            li.put("总金额", String.valueOf(mm.getCost()));
            li.put("订单积分", String.valueOf(mm.getIntegral()));
            li.put("发货状态", mm.getGoods());
            li.put("订单号", mm.getSerialnumber());
            li.put("订单创建时间", DateUtil.formatLocalDateTime(mm.getCreateTime()));
            li.put("订单更新时间", DateUtil.formatLocalDateTime(mm.getUpdateTime()));
            return li;
        }).collect(Collectors.toList());

        writer.merge(0, 0, 0, 9, "订单详情", cellStyle);

        //设置当前行为 第2行
        writer.setCurrentRow(1);

        //自定义标题别名
        writer.addHeaderAlias("序号", "序号");
        writer.addHeaderAlias("用户编号", "用户编号");
        writer.addHeaderAlias("用户名", "用户名");
        writer.addHeaderAlias("用户地址", "用户地址");
        writer.addHeaderAlias("总金额", "总金额");
        writer.addHeaderAlias("订单积分", "订单积分");
        writer.addHeaderAlias("发货状态", "发货状态");
        writer.addHeaderAlias("订单号", "订单号");
        writer.addHeaderAlias("订单创建时间", "订单创建时间");
        writer.addHeaderAlias("订单更新时间", "订单更新时间");

        writer.writeCellValue(0, 1, "序号");
        writer.setStyle(headCellStyle, 0, 1);
        writer.writeCellValue(1, 1, "用户编号");
        writer.setStyle(headCellStyle, 1, 1);
        writer.writeCellValue(2, 1, "用户名");
        writer.setStyle(headCellStyle, 2, 1);
        writer.writeCellValue(3, 1, "用户地址");
        writer.setStyle(headCellStyle, 3, 1);
        writer.writeCellValue(4, 1, "总金额");
        writer.setStyle(headCellStyle, 4, 1);
        writer.writeCellValue(5, 1, "订单积分");
        writer.setStyle(headCellStyle, 5, 1);
        writer.writeCellValue(6, 1, "发货状态");
        writer.setStyle(headCellStyle, 6, 1);
        writer.writeCellValue(7, 1, "订单号");
        writer.setStyle(headCellStyle, 7, 1);
        writer.writeCellValue(8, 1, "订单创建时间");
        writer.setStyle(headCellStyle, 8, 1);
        writer.writeCellValue(9, 1, "订单更新时间");
        writer.setStyle(headCellStyle, 9, 1);

        writer.setColumnWidth(-1, 20);

        writer.setOnlyAlias(true);
        writer.write(collect, true);

        writer.setRowHeight(-1, 30);
        writer.setColumnWidth(-1, 15);
        String filename = excelName + DateUtil.today() + ".xlsx";
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (out != null) {
                IoUtil.close(out);
            }
        }
    }
}
